package com.andy.module.base

import androidx.lifecycle.viewModelScope
import com.andy.module.ext.genLiveData
import com.andy.module.network.ApiPagerResponse
import com.andy.module.network.ApiResponse
import com.andy.module.util.ViewModelUtil
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import com.andy.network.base.BaseViewModel
import com.andy.network.livedata.EventLiveData
import me.hgj.jetpackmvvm.ext.util.loge
import me.hgj.jetpackmvvm.network.ExceptionHandle
import me.hgj.jetpackmvvm.state.ResultState

/**
 * @creator:LuoFan
 * @date:2022/6/6
 * @description:
 */
abstract class BasePageListViewModel<T> : BaseViewModel() {

    var pageNumKey = "pn"

    var pageSizeKey = "ps"

    val params = HashMap<String, Any>()

    val resultList = genLiveData<ApiPagerResponse<T>>()

    var mAdapter: BaseQuickAdapter<T, BaseViewHolder>? = null

    private var pageNum = 1

    private var pageSize = 20

    fun getPageNum() = pageNum

    fun setPageSize(size: Int) {
        pageSize = size
    }

    val listLoadingChange: ListUiLoadingChange by lazy { ListUiLoadingChange() }

    /**
     * 内置封装好的可通知Activity/fragment 结束下拉刷新、结束加载更多
     */
    inner class ListUiLoadingChange {
        //是否下拉刷新
        val isRefresh by lazy { EventLiveData<Boolean>() }

        //是否加载更多
        val isLoadMore by lazy { EventLiveData<Boolean>() }
    }

    fun requestList(isRefresh: Boolean = false): Job {
        params[pageSizeKey] = 20
        if (isRefresh) {
            pageNum = 1
        }
        params[pageNumKey] = pageNum
        return viewModelScope.launch {
            runCatching {
                //请求体
                genRequest()
            }.onSuccess { response ->
                if (response.isSuccess()) {
                    response.getResponseData()?.let {
                        resultList.value = ResultState.onAppSuccess(it)
                        listLoadingChange.isLoadMore.postValue(it.datas.size == pageSize)
                        listLoadingChange.isRefresh.postValue(it.datas.size == pageSize)
                        pageNum++
                    } ?: let {
                        resultList.value = ViewModelUtil.genAppError(response)
                        listLoadingChange.isLoadMore.postValue(true)
                        listLoadingChange.isRefresh.postValue(true)
                    }
                } else {
                    resultList.value = ViewModelUtil.genAppError(response)
                    listLoadingChange.isLoadMore.postValue(true)
                    listLoadingChange.isRefresh.postValue(true)
                }
            }.onFailure {
                ToastUtils.showShort(it.message)
                resultList.value = ResultState.onAppError(ExceptionHandle.handleException(it))
                listLoadingChange.isRefresh.postValue(true)
                listLoadingChange.isLoadMore.postValue(true)
                it.message?.loge()
                //打印错误栈信息
                LogUtils.e(it.message)

            }
        }
    }

    abstract suspend fun genRequest(): ApiResponse<ApiPagerResponse<T>>

}